package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Visitors;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UsersVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo info) {
        QueryWrapper qw = new QueryWrapper();
        //1、用户id列表
        qw.in("id", userIds);
        //2、添加筛选条件
        if (info != null) {
            if (info.getAge() != null) {
                qw.lt("age", info.getAge());
            }
            if (!StringUtils.isEmpty(info.getGender())) {
                qw.eq("gender", info.getGender());
            }
            if (!StringUtils.isEmpty(info.getNickname())) {
                qw.like("nickname", info.getNickname());
            }
        }
        List<UserInfo> list = userInfoMapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }

    @Override
    public IPage findAll(Integer page, Integer pagesize) {
        return userInfoMapper.selectPage(new Page<UserInfo>(page, pagesize), null);
    }

    @Override
    public void changeHeader(UserInfo userInfo) {

        userInfoMapper.updateById(userInfo);
    }

    @Override
    public PageResult findUserByIds(Integer page, Integer pagesize, List<Long> ids, String nickname) {
        IPage iPage = new Page(page, pagesize);

        QueryWrapper qw = new QueryWrapper();
        qw.in("id", ids);
        qw.orderByAsc("created");
        if (nickname != null) {
            qw.like("nickname", nickname);
        }
        //分页查询
        IPage iPage1 = userInfoMapper.selectPage(iPage, qw);
        //对象集合
        List<UserInfo> list = iPage1.getRecords();




        List<UsersVo> vo = new ArrayList<>();
        for (UserInfo info : list) {
            if (info != null) {
                UsersVo usersVo = new UsersVo();
                usersVo.setAlreadyLove(false);
                usersVo.setMatchRate(99);
                usersVo.setId(Math.toIntExact(info.getId()));
                BeanUtils.copyProperties(info, usersVo);
                vo.add(usersVo);
            }
        }
        PageResult pageResult = new PageResult();
        pageResult.setItems(vo);
        return pageResult;
    }


}
